<?php
require('../../lib/base.php');
$error=array();
$base=new base(array('web','advertiser'));
$httpsqs=$base->vars('app','httpsqs');
$url='http://'.$httpsqs['host'].':'.$httpsqs['port'].'/?auth='.$httpsqs['auth'];
$status=file_get_contents($url.'&name=pageview&opt=status_json',0,stream_context_create(array('http'=>array('method'=>'GET','timeout'=>3))));
if(empty($status)){
	echo $url.' error.';
}else{
	$status=json_decode($status,true);
	$num=$status['unread']>$httpsqs['equal']?$httpsqs['equal']:$status['unread'];
	if($num>0){
		$pageview=file_get_contents($url.'&charset=utf-8&name=pageview&opt=get&num='.$num,0,stream_context_create(array('http'=>array('method'=>'GET','timeout'=>3))));
		//将数据出现的浏览器,语言,系统中文替换为相应ID.
		$sort=$base->model('sort');$site=$base->model('site');$config=$base->model('config');
		//提取当前数据中站点
		preg_match_all('/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+/u',$pageview,$matches,PREG_PATTERN_ORDER);
		$matches=array_unique($matches[0]);
		$v=$site->GSite(array('select'=>'id,domain','where'=>array('domain IN'=>$matches)));
		$matches=array_flip($matches);$matches=array_map('is_int',$matches);
		foreach($v as $value){
			$matches[$value['domain']]=$value['id'];
		}
		$v=array_keys($matches,true,true);
		if(!empty($v)){
			$insert=array();
			foreach($v as $value){$insert[]=array($value,$base->runtime);}
			$affect=$site->ISite(array('field'=>array('domain','date'),'value'=>$insert));
			if($affect!=count($insert)){
				echo 'INSERT ERROR.';
				exit();
			}
			$v=$site->GSite(array('select'=>'id,domain','where'=>array('domain IN'=>$v)));
			foreach($v as $value){
				$matches[$value['domain']]=$value['id'];
			}
		}
		$flux=$sort->GSort(array('where'=>array('label IN'=>array('browser','language','system'))));
		$replace=array();
		foreach($flux as $value){
			$replace['pattern'][]='/,'.$value['value'].',/u';
			$replace['replacement'][]=','.$value['id'].',';
		}
		foreach($matches as $key=>$value){
			$replace['pattern'][]='/,'.$key.',/u';
			$replace['replacement'][]=','.$value.',';
		}
		$pageview=preg_replace($replace['pattern'],$replace['replacement'],$pageview);unset($replace);
		$pageview=mysql_escape_string($pageview);
		$pageview=explode('<br/>',$pageview);
		$verify=$base->vars('app','encryption');
		$china=$sort->GSort(array('where'=>array('label='=>'china')));
		foreach($pageview as $key=>$value){
			$value=explode(',',$value);
			if(count($value)==20){
				if(md5($verify.$value[4].','.$value[19].','.$value[6].','.$value[7].','.$value[17])==$value[18]){
					unset($value[17]);unset($value[18]);unset($value[19]);
					if($value[10]!=0){
						$area=$china[$value[10]];
						if($area['parent']==0){
							$value[9]=$area['id'];$value[10]=0;
						}else{
							$value[9]=$area['parent'];
						}
					}
					$value[12]=intval($value[12]);//浏览器版本格式化
					$pageview[$key]=$value;
				}else{
					unset($pageview[$key]);
				}
			}else{
				unset($pageview[$key]);
			}
		}
		if(count($pageview)>0){
			$data=$base->model('data');
			$affect=$data->insert('pageview',array('field'=>array('date','visitor','track','site','billboard','format','usage','modality','advertisers','province','city','browser','version','lang','system','screen','keyword'),'value'=>$pageview));
			if($affect!=count($pageview)){
				$error['pageview']=$pageview;
			}
		}
	}else{
		echo 'pageview is empty.';
	}
}
$status=file_get_contents($url.'&name=track&opt=status_json',0,stream_context_create(array('http'=>array('method'=>'GET','timeout'=>3))));
if(empty($status)){
	echo 'error.';
}else{
	$status=json_decode($status,true);
	$num=$status['unread']>$httpsqs['equal']?$httpsqs['equal']:$status['unread'];
	if($num>0){
		$track=file_get_contents($url.'&charset=utf-8&name=track&opt=get&num='.$num,0,stream_context_create(array('http'=>array('method'=>'GET','timeout'=>3))));
		$track=mysql_escape_string($track);
		$track=explode('<br/>',$track);
		if(count($track)>0){
			$data=$base->model('data');
			foreach($track as $key=>$value){
				$track[$key]=explode(',',$value);
			}
			//插入数据库需要做唯一替换
			$affect=$data->replace('track',array('field'=>array('action','visitor','track','pageview','date'),'value'=>$track));
			if($affect!=count($track)){
				$error['track']=$track;
			}
		}
	}else{
		echo 'track is empty.';
	}
}
if(!empty($error)){echo serialize($error);}
?>